package com.ruoyi.keep.service.impl;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.keep.mapper.UserBuyNumberCardMapper;
import com.ruoyi.keep.domain.UserBuyNumberCard;
import com.ruoyi.keep.service.IUserBuyNumberCardService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户购买次数卡Service业务层处理
 *
 * @author wxy
 * @date 2024-08-14
 */
@Service
public class UserBuyNumberCardServiceImpl implements IUserBuyNumberCardService {
    @Autowired
    private UserBuyNumberCardMapper userBuyNumberCardMapper;

    /**
     * 查询用户购买次数卡
     *
     * @param id 用户购买次数卡主键
     * @return 用户购买次数卡
     */
    @Override
    public UserBuyNumberCard selectUserBuyNumberCardById(Long id) {
        return userBuyNumberCardMapper.selectUserBuyNumberCardById(id);
    }
    @Override
    public UserBuyNumberCard selectUserBuyNumberCardByUserId(Long userId) {
        return userBuyNumberCardMapper.selectUserBuyNumberCardByUserId(userId);
    }

    /**
     * 查询用户购买次数卡列表
     *
     * @param userBuyNumberCard 用户购买次数卡
     * @return 用户购买次数卡
     */
    @Override
    public List<UserBuyNumberCard> selectUserBuyNumberCardList(UserBuyNumberCard userBuyNumberCard) {
        return userBuyNumberCardMapper.selectUserBuyNumberCardList(userBuyNumberCard);
    }

    /**
     * 新增用户购买次数卡
     *
     * @param userBuyNumberCard 用户购买次数卡
     * @return 结果
     */
    @Override
    public int insertUserBuyNumberCard(UserBuyNumberCard userBuyNumberCard) {
        return userBuyNumberCardMapper.insertUserBuyNumberCard(userBuyNumberCard);
    }

    /**
     * 修改用户购买次数卡
     *
     * @param userBuyNumberCard 用户购买次数卡
     * @return 结果
     */
    @Override
    @Transactional
    public int updateUserBuyNumberCard(UserBuyNumberCard userBuyNumberCard) {
        UserBuyNumberCard numberCard = userBuyNumberCardMapper.selectUserBuyNumberCardByUserId(userBuyNumberCard.getUserId());
        Integer oldResidueNumber = numberCard.getResidueNumber();
        System.out.println(oldResidueNumber);
        System.out.println("++++++++++++++++++++++++++++++");
        if (oldResidueNumber >= 1){
            Integer newResidueNumber = oldResidueNumber + userBuyNumberCard.getResidueNumber();
            userBuyNumberCard.setResidueNumber(newResidueNumber);
            return userBuyNumberCardMapper.updateUserBuyNumberCard(userBuyNumberCard);
        }else {
            throw new RuntimeException();
        }

    }

    /**
     * 批量删除用户购买次数卡
     *
     * @param ids 需要删除的用户购买次数卡主键
     * @return 结果
     */
    @Override
    public int deleteUserBuyNumberCardByIds(Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        return userBuyNumberCardMapper.deleteBatchIds(idList);
    }

    /**
     * 删除用户购买次数卡信息
     *
     * @param id 用户购买次数卡主键
     * @return 结果
     */
    @Override
    public int deleteUserBuyNumberCardById(Long id) {
        return userBuyNumberCardMapper.deleteById(id);
    }
}
